import Vue from 'vue';
import axios from 'axios';
import VueAxios from 'vue-axios';
import urls from "./urls";
import route from "../router/index";
import {Message,Loading} from "element-ui";

// import store from '@/store/index.js';



axios.defaults.timeout = 2000;
axios.defaults.baseURL = urls.baseUrl;
axios.defaults.method="post";
 // axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
axios.defaults.headers["Content-Type"] = "application/json;charset=UTF-8";
// axios.defaults.headers["Content-Type"] = "application/x-www-form-urlencoded";
// 'Content-Type':"application/x-www-form-urlencoded";
// 'Content-Type':"application/json";
// 	"multipart/form-data"
axios.interceptors.request.use(function(config) {
	// Loading.service(true)
	// config.url = "/api" + config.url;
	// let UserInfo = store.state.userInfo;
	// if (UserInfo) {
	// 	config.headers.Authorization = UserInfo.token
	// }
	return config;
}, function(error) {

	// 对请求错误做些什么
	return Promise.reject(error);
});

// 添加响应拦截器
axios.interceptors.response.use(function(response) {
	// 对响应数据做点什么
	const data = response.data;
	const code = response.data.code;
	// Loading.service(true).close()
	if (code == 200) {
		return Promise.resolve(data);
	} else if (code == 403) {
		route.push("/login")
	} else {
		Message.error(response.data.message)
		return Promise.reject(data);
	}
}, function(error) {
	 // Loading.service(true).close()
	 Message.error("网络出错了")
	// 对请求错误做些什么
	return Promise.reject(error);
})


Vue.use(VueAxios, axios)
